<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib uri="/WEB-INF/tld/liferay-portlet.tld" prefix="portlet"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="mytag" %>

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="javax.portlet.*"%>

<portlet:defineObjects />

<html>
	<head>
		<script>
		var selectedReportId="";
		var selectedReportFrm="";
        var completedReportFrm="";

        var ReportManaget = {
            deleteMyReport : function (elem){
                var answer = confirm (ErrorMessages.DELETE_CONFIRM);
                if (answer){
                    gotoSubmit(elem, 'reportManager');
                }
            },

           init : function(managerContainer){
                preConfig('report_manager','reportManager','${HELP_URL}');
                var $myRepTable = $("#myReportsContentTable");
                var $completedRepTable=$("#completedReportsContentTable");

				$myRepTable.tablesorter({widthFixed: false, widgets: ['zebra']});
                $($("th",$myRepTable).get(1)).width($($("tbody tr:first td",$myRepTable).get(1)).width());
                $($("th",$myRepTable).get(0)).width($($("tbody tr:first td",$myRepTable).get(0)).width());
                $("tbody tr",$myRepTable).dblclick(function() {
					gotoSubmit($("#editBtn")[0], 'reportManager');
				});
				$("tbody tr",$myRepTable).click(function(){
					$("tbody tr",$myRepTable).removeClass('selected');
					$(this).addClass('selected');
					$('#editBtn, #deleteBtn, #runNowBtn').removeAttr("disabled").removeClass("disabled");
					selectedReportId=$(this).attr('id');
					selectedReportFrm=$(this).attr('frm');
				});

                $completedRepTable.tablesorter({widthFixed: false, widgets: ['zebra']});
                $($("th",$completedRepTable).get(1)).width($($("tbody tr:first td",$completedRepTable).get(1)).width());
                $($("th",$completedRepTable).get(0)).width($($("tbody tr:first td",$completedRepTable).get(0)).width());

                $("tbody tr",$completedRepTable).dblclick(function() {
					gotoSubmit($("#viewCompletedReportBtn")[0], 'reportManager');
				});

                $("tbody tr",$completedRepTable).click(function(){
					$("tbody tr",$completedRepTable).removeClass('selected');
					$(this).addClass('selected');

                    $('#deleteCompletedReportBtn, #viewCompletedReportBtn')
                            .data('completedreportname', $(this).data('completedreportname'))
                            .removeAttr("disabled")
                            .removeClass("disabled");
                    completedReportFrm=$(this).data('type');
				});

                $(window).resize(function(){
                    $($("th",$myRepTable).get(0)).width($($("tbody tr:first td",$myRepTable).get(0)).width());
                    $($("th",$myRepTable).get(1)).width($($("tbody tr:first td",$myRepTable).get(1)).width());
                    $($("th",$completedRepTable).get(0)).width($($("tbody tr:first td",$completedRepTable).get(0)).width());
                    $($("th",$completedRepTable).get(1)).width($($("tbody tr:first td",$completedRepTable).get(1)).width());
                });

                $('#newBtn, #editBtn, #runNowBtn, #viewCompletedReportBtn').click(function(){
                    gotoSubmit($(this)[0], 'reportManager');
                });
                $('#deleteCompletedReportBtn, #deleteBtn').click(function(){
                    ReportManaget.deleteMyReport($(this)[0]);
                });
            }
        };

		$(document).ready(function(){
                ReportManaget.init();
				if (document.getElementById('isRunNowPressed').value == 'true'){
					document.getElementById('isRunNowPressed').value == 'false';
					location.href = '<portlet:resourceURL><portlet:param name="whoRuns" value="report" /></portlet:resourceURL>';
				}
				if (document.getElementById('isOpenCompletedReport').value == 'true'){
					document.getElementById('isOpenCompletedReport').value == 'false';
					location.href = '<portlet:resourceURL><portlet:param name="whoRuns" value="completedReport" /></portlet:resourceURL>';
				}
			}
		);
		</script>
	</head>
	<body>
		<form id="report_manager" method="post">
			<div id="container">
				<div class="spacer" style="width: 759px;"></div>
				<div class="loadingMaskContainer">
					<div id="loadingMask"></div>
					<div class="aui-loadingmask-message"><div class="aui-loadingmask-message-content">Loading...</div></div>
				</div>
				<div class="myReports">
					<div class="reportsLabel">My Reports</div>
					<div class="reportsContent">
						<div class="reportsButtons">
							<input type="button" id="newBtn" class="button" value="New" />
							<input type="button" id="editBtn" class="button disabled" value="Edit" disabled />
							<input type="button" id="deleteBtn" class="button disabled" value="Delete" disabled />
							<input type="button" id="runNowBtn" class="button disabled" value="Run Now" disabled />
						</div>
						<div class="reportsMainContent">
							<table id="myReportsContentTable" class="reportsTable">
								<thead>
									<tr>
										<th class="header"><div class="headerName">Name</div></th>
										<th class="header"><div class="headerReport">Report</div></th>
										<th class="header"><div class="headerSchedule">Schedule</div></th>
										<th class="header"><div class="hederRun">Next Run</div></th>
									</tr>
								</thead>
								<tbody>
								<c:forEach var="myreport" items="${MY_REPORTS}">
									<tr id="<c:out value='${myreport.id}' />" frm="<c:out value='${myreport.formatName}' />">
										<td class="myReportNameTd"><div class="headerName"><c:out value='${myreport.name}' /></div></td>
										<td class="mainTd"><c:out value='${myreport.reportName}' /></td>
										<td><div class="headerSchedule"><c:out value='${myreport.scheduleType}' /></div></td>
										<td><div class="hederRun"><c:out value='${myreport.scheduleNextRun}' /></div></td>
									</tr>
								</c:forEach>
								</tbody>
							</table>
						</div>
                        <div class="footerText">Click New to define a new report or select existing report definition to edit, delete or run</div>
					</div>
				</div>
				<div class="completedReports">
					<div class="reportsLabel">Completed Reports</div>
					<div class="reportsContent">
						<div class="reportsButtons">
							<input type="button" id="viewCompletedReportBtn" data-completedreportname="" class="button disabled" disabled value="View" />
							<input type="button" id="deleteCompletedReportBtn" data-completedreportname="" class="button disabled" disabled value="Delete" />
						</div>
						<div class="reportsMainContent">
							<table id="completedReportsContentTable" class="reportsTable">
								<thead>
									<tr>
										<th class="header"><div class="headerName">Name</div></th>
										<th class="header"><div class="headerReport">Report</div></th>
										<th class="header"><div class="hederRun">Run Date/Time</div></th>
									</tr>
								</thead>
								<tbody>
								<c:forEach var="compreport" items="${COMPLETED_REPORTS}">
									<tr data-type="<c:out value='${compreport.type}' />" data-completedreportname="<c:out value='${compreport.name}' />.<c:out value='${compreport.reportName}' />.<c:out value='${compreport.type}' />">
										<td class="reportNameTd"><div class="headerName"><c:out value='${compreport.myReportName}' /></div></td>
										<td class="mainTd"><c:out value='${compreport.reportName}' /></td>
										<td><div class="hederRun"><c:out value='${compreport.timeCreated}' /></div></td>
									</tr>
								</c:forEach>
								</tbody>
							</table>
						</div>
                        <div class="footerText">Select existing report instance to view or delete</div>
					</div>
				</div>
				<input id="formAction" type="hidden" value="<portlet:actionURL/>" />
				<input type="hidden" id="isRunNowPressed" value=<c:out value="${isRunNowPressed}"/> />
				<input type="hidden" id="isOpenCompletedReport" value=<c:out value="${isOpenCompletedReport}"/> />
			</div>
		</form>
	</body>
</html>
